package com.jrh.web.admin;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jrh.biz.img.ImgUploadService;
import com.jrh.biz.label.LabelService;
import com.jrh.biz.showroom.ShowroomService;
import com.jrh.domain.common.Page;
import com.jrh.domain.entity.Label;
import com.jrh.domain.entity.Showroom;
import com.jrh.web.admin.form.AdminFormCopier;
import com.jrh.web.admin.form.AdminShowroomForm;


@Controller
@RequestMapping("/admin/showroom")
public class AdminShowroomController {
	
	private static final String  UPLOAD_TYPE = "showroom";
	private static final Integer pageSize    = 5;
	    
	@Autowired
	private ShowroomService showroomService;
	@Autowired
	private LabelService labelService;
	@Autowired
	private ImgUploadService imgUploadService;
//	@Autowired
//    private CategoryDao categoryDao;

	@RequestMapping("/list")
	public String index(Integer pageIndex, Model model){
		Showroom showroom = new Showroom();
        if (pageIndex == null) {
            pageIndex = 1;
        }
        Integer start = (pageIndex - 1) * pageSize;
		Page<Showroom> list = showroomService.listShowroomsByCondition(showroom, start, pageSize);
		model.addAttribute("list",list);
		model.addAttribute("spaces",labelService.getList(Label.LABEL_SPACE_KEY));
//		model.addAttribute("spaces", categoryDao.listParentCategory());
		model.addAttribute("styles",labelService.getList(Label.LABEL_STYLE_KEY));
		model.addAttribute("seriess",labelService.getList(Label.LABEL_SHOWROOMSET_KEY));
		return "admin/showroom/list";
	}
	
	@RequestMapping("/update/{id}")
	public String update(@PathVariable Long id,Model model) {
		model.addAttribute("showroom",showroomService.getShowroomById(id));
		model.addAttribute("spaces",labelService.getList(Label.LABEL_SPACE_KEY));
//		model.addAttribute("spaces", categoryDao.listParentCategory());
		model.addAttribute("styles",labelService.getList(Label.LABEL_STYLE_KEY));
		model.addAttribute("seriess",labelService.getList(Label.LABEL_SHOWROOMSET_KEY));
		return "admin/showroom/update";
	}
	
	@RequestMapping(method = RequestMethod.POST, value="/update")
	public String doUpdate(@ModelAttribute("showroomForm")AdminShowroomForm showroomForm,String oldPicPath, Model model) {
		if(showroomForm.getMainPic().isEmpty()){
			showroomForm.setMainPicPath(oldPicPath);
		}else{
			String mainPicPath	= imgUploadService.upload(showroomForm.getMainPic(), UPLOAD_TYPE);
			String smallPicPath = imgUploadService.resize(mainPicPath, 120, 80);
			showroomForm.setMainPicPath(mainPicPath);
			showroomForm.setSmallPicPath(smallPicPath);
		}
		Showroom showroom = AdminFormCopier.convert(showroomForm, null);
		showroom.setGmtModified(new Date());
		if(showroomService.update(showroom)){
			model.addAttribute("success",Boolean.TRUE.toString());
		}else{
			model.addAttribute("success",Boolean.FALSE.toString());
		}
		return index(1,model);
	}
	
	@ResponseBody
	@RequestMapping("/delete/{id}")
	public String delete(@PathVariable Long id) {
		try {
			showroomService.delete(id);
			return Boolean.TRUE.toString();
		} catch (Exception e) {
			return Boolean.FALSE.toString();
		}
	}
	
	@RequestMapping("/query")
	public String query(HttpServletRequest request, Integer pageIndex, Model model) {
		Showroom showroom = new Showroom();
		showroom.setSpace(request.getParameter("space"));
		showroom.setStyle(request.getParameter("style"));
		showroom.setSeries(request.getParameter("series"));
		showroom.setName(request.getParameter("name"));
		if (pageIndex == null) {
			pageIndex = 1;
		}
        Integer start = (pageIndex - 1) * pageSize;
		Page<Showroom> list = showroomService.listShowroomsByCondition(showroom, start, pageSize);
		model.addAttribute("list", list);
		model.addAttribute("spaces",labelService.getList(Label.LABEL_SPACE_KEY));
//		model.addAttribute("spaces", categoryDao.listParentCategory());
		model.addAttribute("styles",labelService.getList(Label.LABEL_STYLE_KEY));
		model.addAttribute("seriess",labelService.getList(Label.LABEL_SHOWROOMSET_KEY));
		return "admin/showroom/list"; 
	}
	
	@RequestMapping("/detail/{id}")
	public String view(@PathVariable Long id, Model model) {
		model.addAttribute("showroom",showroomService.getShowroomById(id));
		return "admin/showroom/detail"; 
	}

	@RequestMapping("/add")
	public String addView(Model model) {
		model.addAttribute("spaces",labelService.getList(Label.LABEL_SPACE_KEY));
//		model.addAttribute("spaces", categoryDao.listParentCategory());
		model.addAttribute("styles",labelService.getList(Label.LABEL_STYLE_KEY));
		model.addAttribute("seriess",labelService.getList(Label.LABEL_SHOWROOMSET_KEY));
		return "admin/showroom/add"; 
	}
	
	@RequestMapping(method = RequestMethod.POST, value="/add")
	public String add(@ModelAttribute("showroomForm")AdminShowroomForm showroomForm, Model model) {
		String mainPicPath	= imgUploadService.upload(showroomForm.getMainPic(), UPLOAD_TYPE);
		String smallPicPath = imgUploadService.resize(mainPicPath, 120, 80);
		showroomForm.setMainPicPath(mainPicPath);
		showroomForm.setSmallPicPath(smallPicPath);
		Showroom showroom = AdminFormCopier.convert(showroomForm, null);
		showroom.setGmtCreate(new Date());
		showroom.setGmtModified(new Date());
		try {
			showroomService.addShowroom(showroom);
			model.addAttribute("success",Boolean.TRUE.toString());
		} catch (Exception e) {
			model.addAttribute("success",Boolean.FALSE.toString());
		}
		return "admin/showroom/add"; 
	}
	
}
